package com.szx.usercenter.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.szx.usercenter.domain.FilePan;
import com.szx.usercenter.handle.CenterExceptionHandler;
import com.szx.usercenter.service.FilePanService;
import com.szx.usercenter.util.FileUploadEnum;
import com.szx.usercenter.util.JwtHelper;
import com.szx.usercenter.util.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @author songzx
 * @date 2024/6/2
 * @apiNote
 */
@Api(tags = "云盘管理")
@RestController
@RequestMapping("/pan")
public class FilePanController {
    @Resource
    FilePanService filePanService;

    @ApiOperation("分片上传")
    @PostMapping("chunkUpload")
    public Response upload(
            MultipartFile chunkFile,
            String fileName,
            Long fileSize,
            String filePid,
            Integer chunkNum,
            Integer totalChunk,
            HttpServletRequest request) {

        FileUploadEnum fileUploadEnum =
                filePanService.uploadChunkFile(
                        chunkFile, fileName, fileSize, filePid, chunkNum, totalChunk, request);

        return Response.rspData(fileUploadEnum);
    }

    @ApiOperation("获取文件列表，返回成树形结构")
    @GetMapping("getFileListTree")
    public Response getFileList(
            String fileName,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date updateTime,
            HttpServletRequest request) {
        return filePanService.getFileList(fileName, updateTime, request);
    }

    @ApiOperation("获取一层文件列表")
    @GetMapping("getFileList")
    public Response getFileList(
            String fileName,
            @RequestParam(required = true, defaultValue = "0") String filePid,
            HttpServletRequest request) {
        LambdaQueryWrapper<FilePan> qw = new LambdaQueryWrapper<>();

        qw.like(StrUtil.isNotEmpty(fileName), FilePan::getFileName, fileName)
                .eq(FilePan::getFilePid, filePid)
                .eq(FilePan::getUserId, JwtHelper.getLoginId(request))
                .orderByDesc(FilePan::getUpdateTime);

        return Response.rspData(filePanService.list(qw));
    }

    @ApiOperation("新建文件夹")
    @PostMapping("addDir")
    public Response addDir(@RequestBody FilePan filePan, HttpServletRequest request) {
        boolean b = filePanService.addDir(filePan, request);
        return b ? Response.success() : Response.error();
    }

    @ApiOperation("删除文件")
    @PostMapping("deleteFile")
    public Response deleteFile(@RequestBody FilePan filePan, HttpServletRequest request) {
        filePanService.deleteFile(filePan, request);
        return Response.success();
    }

    @ApiOperation("文件预览")
    @GetMapping("preview/{fileId}")
    public void preview(@PathVariable String fileId, HttpServletResponse response) {
        if (fileId.endsWith(".ts")) {
            filePanService.readFileTs(fileId, response);
        } else {
            LambdaUpdateWrapper<FilePan> qw = new LambdaUpdateWrapper<>();
            qw.eq(FilePan::getFileId, fileId);
            FilePan one = filePanService.getOne(qw);
            if (ObjectUtil.isEmpty(one)) {
                throw new CenterExceptionHandler("文件不存在");
            }
            filePanService.preview(one, response);
        }
    }

    @ApiOperation("文件重命名")
    @PostMapping("renameFileName")
    public Response renameFileName(@RequestBody FilePan filePan, HttpServletRequest request) {
        boolean b = filePanService.renameFileName(filePan, request);
        return b ? Response.success() : Response.error();
    }

    @ApiOperation("文件下载")
    @GetMapping("downloadFile/{fileId}")
    public void downloadFile(@PathVariable String fileId, HttpServletResponse response) {
        filePanService.download(fileId, response);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("batchDelete")
    public Response batchDelete(String fileIds, HttpServletRequest request) {
        boolean b = filePanService.batchDelete(fileIds, request);
        return b ? Response.success() : Response.error();
    }
}
